<template>
	<md-card style="margin:8px 0;">
		<md-card-header md-alignment="space-between" style="display:flex;">
			<span class="md-title md-title-text" :title="handleName+getLangText(label.handleStyle)">{{handleName}}{{getLangText(label.handleStyle)}}</span>
			<span style="flex:1;text-align:right;">
				<!-- <md-button class="md-icon-button" @click="sendStyle" v-show="isShow">
					<i class="iconfont iconsave"></i>
				</md-button> -->
				<md-button
					:class="['md-icon-button',isShow?'md-primary':'']"
					@click="sendStyle"
				>
					<i :class="['iconfont', isShow?'iconsave':'iconshezhi2']"></i>
				</md-button>
			</span>
		</md-card-header>
		<md-card-content v-if="isShow">
			<div class="table-row-item">
				<span>{{getLangText(label.color)}}:</span>
				<span>
					<span
						class="color-span"
						:style="{background:handleStyle.color}"
						@click="showColorPicker('color')"
					></span>
					{{handleStyle.color}}
				</span>
			</div>

			<color-picker
				:lang-type="langType"
				:rgba="handleStyle.color"
				@color-result="onColorPicker"
				:show-picker="picker.show&&picker.current=='color'"
			></color-picker>

			<div class="table-row-item">
				<span>{{getLangText(label.borderColor)}}:</span>
				<span>
					<span
						class="color-span"
						:style="{background:handleStyle.borderColor}"
						@click="showColorPicker('borderColor')"
					></span>
					{{handleStyle.borderColor}}
				</span>
			</div>

			<color-picker
				:lang-type="langType"
				:rgba="handleStyle.borderColor"
				@color-result="onColorPicker"
				:show-picker="picker.show&&picker.current=='borderColor'"
			></color-picker>

			<md-field>
				<label>{{getLangText(label.borderType)}}</label>
				<md-select v-model="handleStyle.borderType">
					<md-option :value="item.en" v-for="item in types" :key="item.en">{{getLangText(item)}}</md-option>
				</md-select>
			</md-field>

			<md-field>
				<label>{{getLangText(label.borderWidth)}}</label>
				<md-input v-model="handleStyle.borderWidth"></md-input>
			</md-field>

			<md-field>
				<label>{{getLangText(label.opacity)}}</label>
				<md-input v-model="handleStyle.opacity"></md-input>
			</md-field>

			<md-field>
				<label>{{getLangText(label.shadowBlur)}}</label>
				<md-input v-model="handleStyle.shadowBlur"></md-input>
			</md-field>

			<div class="table-row-item">
				<span>{{getLangText(label.shadowColor)}}:</span>
				<span>
					<span
						class="color-span"
						:style="{background:handleStyle.shadowColor}"
						@click="showColorPicker('shadowColor')"
					></span>
					{{handleStyle.shadowColor}}
				</span>
			</div>

			<color-picker
				:lang-type="langType"
				:rgba="handleStyle.shadowColor"
				@color-result="onColorPicker"
				:show-picker="picker.show&&picker.current=='shadowColor'"
			></color-picker>

			<md-field>
				<label>{{getLangText(label.shadowOffsetX)}}</label>
				<md-input v-model="handleStyle.shadowOffsetX"></md-input>
			</md-field>
			<md-field>
				<label>{{getLangText(label.shadowOffsetY)}}</label>
				<md-input v-model="handleStyle.shadowOffsetY"></md-input>
			</md-field>
		</md-card-content>
	</md-card>
</template>
<script>
	import ColorPicker from "@/components/comp/ColorPicker";
	export default {
		name: "HandleStyle",
		props: {
			langType: {
				type: String,
				default: window.localStorage.getItem("langType")
			},
			handleName: {
				type: String,
				default: ""
			},
			option:{
				type:Object,
				default:function () {
					return {};
				}
			}
		},
		components: {
			"color-picker": ColorPicker
		},
		data: () => ({
			label: {
				handleStyle: { zh: "手柄样式", en: "HandleStyle" },
				color: { zh: "图形颜色", en: "color" },
				borderColor: { zh: "描边颜色", en: "borderColor" },
				borderType: { zh: "描边类型", en: "borderType" },
				borderWidth: { zh: "描边宽", en: "borderWidth" },
				shadowBlur: { zh: "阴影模糊大小", en: "shadowBlur" },
				shadowColor: { zh: "阴影颜色", en: "shadowColor" },
				shadowOffsetX: { zh: "阴影水平偏移", en: "shadowOffsetX" },
				shadowOffsetY: { zh: "阴影垂直偏移", en: "shadowOffsetY" },
				opacity: { zh: "图形透明度", en: "opacity" }
			},
			isShow: false,
			handleStyle: {
				color: "#a7b7cc",
				borderColor: "#000",
				borderType: "solid",
				borderWidth: 0,
				shadowBlur: "",
				shadowColor: "",
				shadowOffsetX: 0,
				shadowOffsetY: 0,
				opacity:1
			},
			types: [
				{ zh: "实线", en: "solid" },
				{ zh: "虚线", en: "dashed" },
				{ zh: "点线", en: "dotted" }
			],
			picker: {
				show: false,
				current: ""
			},
			originOption:'',
			isParse:false
		}),
		watch:{
			isShow() {
				if (this.isShow) {
					this.parseObj();					
				}else{
					this.picker.show = false;
				}

			},
			handleStyle:{
				deep:true,
				handler(){
					if(this.isShow&&!this.isParse)
					this.$emit("handle-style-result", JSON.parse(JSON.stringify(this.handleStyle)));
				}
			}
		},
		methods: {
			showColorPicker(tag) {
				if (tag != this.picker.current || !this.picker.show) {
					this.picker.current = tag;
					this.picker.show = true;
				} else {
					this.picker.show = false;
				}
			},
			onColorPicker(op) {
				if(this.picker.current)
				this.handleStyle[this.picker.current] = op.rgba;
			},
			sendStyle() {
				this.isShow = this.isShow?false:true;
				if(!this.isShow){
				this.$emit("handle-style-result", JSON.parse(JSON.stringify(this.handleStyle)));
				}
				
			},
			
			parseObj(){
				if(!this.option||JSON.stringify(this.option)=='{}')return;
				this.isParse=true;	
				for(var key in this.option){
					this.handleStyle[key]=this.option[key];
				}
				this.originOption=JSON.stringify(this.handleStyle);
				var that=this;
				setTimeout(() => {
					that.isParse=false;
				}, 100);
			},
			colseOption(){
				this.isShow=false;
				if (this.originOption!=JSON.stringify(this.handleStyle)) {	
			
			this.$emit("handle-style-result", JSON.parse(JSON.stringify(this.handleStyle)));
				}
			},
			listenEvent(){
				this.app.$on('close-option',this.colseOption);
			},
			offEvent(){
			this.app.$off('close-option',this.colseOption);
			}
		},
		mounted() {
			this.listenEvent();
			
		},
		beforeDestroy(){
			this.offEvent();
		}
	};
</script>
